package com.hyts.bmsp.web.system;

import java.util.ArrayList;
import java.util.List;


import com.alibaba.fastjson.JSONObject;
import com.hyts.bmsp.model.resource.MenuInfoBean;
import com.hyts.bmsp.model.resource.MenuInfoDTO;
import com.hyts.bmsp.model.resource.MenuInfoQueryDTO;
import com.hyts.bmsp.controller.BaseController;
import com.hyts.bmsp.controller.WebController;
import com.hyts.bmsp.bean.ResultJsonBean;
import com.hyts.bmsp.service.resource.*;
import com.hyts.bmsp.tool.ClassRuleUtil;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;

/**
 * @ClassName: MenuController
 * @Description: 菜单管理操作总体控制器
 * @author: LiBo/Alex Lee
 * @date: 2017年9月20日 上午12:30:43
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public class MenuController extends BaseController {

	/**
	 * @fieldName: logger
	 * @fieldType: Log
	 * @Description: 日志输出器
	 */
	private static final Log logger = Log.getLog(MenuController.class);

	private MenuInfoService service = new MenuInfoServiceImpl();
	
	  /**
	 * @Title: index
	 * @Description: 操作服务数据
	 * @return: void
	 */
	  public void index()
	 {
	    toJsp("/admin/system/menu_index");
	 }
	 
	  
	  /**
	 * @Title: showData
	 * @Description: 展现数据信息
	 * @param
	 * @return: void
	 */
	public void showData(){
		MenuInfoQueryDTO dto = new MenuInfoQueryDTO();
		JSONObject obj = new JSONObject();
		try {
			this.injectRequestParamToObj(dto);
			Page<MenuInfoBean> beans = service.searchMenuInfos(dto);
			List<MenuInfoDTO> beanList = new ArrayList<MenuInfoDTO>(beans.getList().size());
			for(int index = 0 ; index<beans.getList().size() ; index++)
			{
				beanList.add(ClassRuleUtil.db2JavaClass(beans.getList().get(index),new MenuInfoDTO()));
			}
			obj.put("success", true);
			obj.put("data", beanList);
			obj.put("totalPage",beans.getTotalPage());
			obj.put("totalRow",beans.getTotalRow());
			toJson(obj);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			obj.put("success", false);
			obj.put("message","查询菜单失败!");
			toJson(obj);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			obj.put("success", false);
			obj.put("message","查询菜单失败!");
			toJson(obj);
		}
	  } 
	  
	  public void add()
	 {
	    toJsp("/admin/system/menu_add");
	 }
	  
	  
	 /**
	 * @Title: doAdd
	 * @Description: 执行添加菜单功能
	 * @return: void
	 */
	 public void doAdd()
	{
		MenuInfoDTO dto = new MenuInfoDTO();
		ResultJsonBean result = new ResultJsonBean(true,"保存菜单成功!");
		try {
			this.injectRequestParamToObj(dto);
			boolean flag = service.addMenuInfo(dto);
			if(!flag){
				result.setSuccess(false);
				result.setMessage("保存菜单失败!");
			}
		} catch (IllegalArgumentException e) {
			result.setSuccess(false);
			result.setMessage("系统异常，保存菜单失败!");
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			result.setSuccess(false);
			result.setMessage("系统异常，保存菜单失败!");
			e.printStackTrace();
		}
		this.toJson(result);
	}
	 
	  public void remove()
	  {
		  
		  
	  }
	  
	  public void edit() {
	    toJsp("/admin/system/menu_edit");
	  }
	
}
